package com.globallogic.strelchuk.multi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.InputMismatchException;

import com.globallogic.strelchuk.multi.entity.AbstractMultiplicationTable;
import com.globallogic.strelchuk.multi.entity.SquareMultiplicationTable;
import com.globallogic.strelchuk.multi.entity.TriangleMultiplicationTable;
import com.globallogic.strelchuk.multi.exception.TableSizeException;

/**
 * @author olena.strelchuk
 */

/**
 * Generate and output Multiplication Table for selected number
 */
public class MultiplicationApplication {

	private static final String EXIT_SYMBOL = "q";
	private static final String INPUT_SHOULD_BE_IN_NUMERIC_FORMAT = "Input should be in numeric format.";
	private static final String NUMBER_IS_EXPECTED_IN_INPUT = "Number is expected in input.";
	private static final String EXIT_MESSAGE = "Exit";
	private static final String TRIANGLE_TABLE = "------------- Triangle Table -------------";
	private static final String SQUARE_TABLE = "-------------- Square Table --------------";
	private static final String DELIMITER = "===============================\n";
	private static final String MENU_MESSAGE = "1:  Create Multiplication Table\n"
			+ "q:  Quit\n";
	private static final String MAKE_YOUR_CHOICE = "Make your choice";
	private static final String ENTER_NUMBER = "Enter number for Multiplication Table:";

	/**
	 * Create the main menu
	 * 
	 * @param keyIn
	 * @throws IOException
	 * @throws NumberFormatException
	 */
	private static void getMenu(BufferedReader keyIn) throws IOException {
		try {
			System.out.println(new StringBuffer().append(DELIMITER)
					.append(MENU_MESSAGE).append(DELIMITER)
					.append(MAKE_YOUR_CHOICE).toString());
			String cmd = keyIn.readLine();
			if (cmd.endsWith(EXIT_SYMBOL)) { // exit if q is entered
				System.out.println(EXIT_MESSAGE);
				System.exit(0);
			}

			System.out.println(ENTER_NUMBER);
			int tableNum = Integer.parseInt(keyIn.readLine());

			AbstractMultiplicationTable table = new SquareMultiplicationTable(tableNum);
			AbstractMultiplicationTable triangleTable = new TriangleMultiplicationTable(
					tableNum);
			System.out.println(SQUARE_TABLE);
			System.out.println(table.toString());
			System.out.println(TRIANGLE_TABLE);
			System.out.println(triangleTable.toString());

		} catch (InputMismatchException e) {
			System.out.println(NUMBER_IS_EXPECTED_IN_INPUT + e);
		} catch (NumberFormatException e) {
			System.out.println(INPUT_SHOULD_BE_IN_NUMERIC_FORMAT + e);
		} catch (IllegalArgumentException e) {
			System.out.println(e.getMessage());
		} catch (TableSizeException e) {
			System.out.println(e.getMessage());
		}
	}

	public static void main(String[] args) throws IOException {

		try {
			// Arrange menu and making of choice
			BufferedReader keyIn = new BufferedReader(new InputStreamReader(
					System.in));

			while (true) {
				// Show menu
				getMenu(keyIn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
